﻿using Leeder.Logger;
using LeederGlobal;
using SPSAndong.Business;
using SPSAndong.Comm;
using SPSAndong.Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace SPSAndong.Forms
{
    public partial class FrmVoiceSet : UIPage
    {
        public LogOperate LogObj => Log.GetLogger(this.GetType());
        AutoSizeFormClass asc = new AutoSizeFormClass();
        bool isLoad = true;      //判断窗体是否首次加载

        List<MusicSetModel> musicSetList;

        Dictionary<int, UITextBox> DicTextAreaNames = new Dictionary<int, UITextBox>();
        Dictionary<int, UIIntegerUpDown> DicIntMusicNos = new Dictionary<int, UIIntegerUpDown>();
        Dictionary<int, UISwitch> DicSwMusicNFs = new Dictionary<int, UISwitch>();
        Dictionary<int, UIIntegerUpDown> DicIntMusictimes = new Dictionary<int, UIIntegerUpDown>();
        Dictionary<int, UISwitch> DicSwMusicIsPlayAlongs = new Dictionary<int, UISwitch>();

        MusicSetBLL musicSetBLL = new MusicSetBLL();

        public FrmVoiceSet()
        {
            InitializeComponent();
        }

        private void FrmVoiceSet_Load(object sender, EventArgs e)
        {
            if (!isLoad)
                return;
            isLoad = false;
            asc.controllInitializeSize(this);
            Init_Control();
            LoadMusicSet();
        }

        private void FrmVoiceSet_SizeChanged(object sender, EventArgs e)
        {
            asc.controlAutoSize(this);
        }

        private void Init_Control()
        {
            DicTextAreaNames.Add(1, txtAreaName1);
            DicTextAreaNames.Add(2, txtAreaName2);
            DicTextAreaNames.Add(3, txtAreaName3);
            DicTextAreaNames.Add(4, txtAreaName4);
            DicTextAreaNames.Add(5, txtAreaName5);
            DicTextAreaNames.Add(6, txtAreaName6);
            DicTextAreaNames.Add(7, txtAreaName7);
            DicTextAreaNames.Add(8, txtAreaName8);
            DicTextAreaNames.Add(9, txtAreaName9);
            DicTextAreaNames.Add(10, txtAreaName10);
            DicTextAreaNames.Add(11, txtAreaName11);
            DicTextAreaNames.Add(12, txtAreaName12);

            DicIntMusicNos.Add(1,  intMusicNo1);
            DicIntMusicNos.Add(2,  intMusicNo2);
            DicIntMusicNos.Add(3,  intMusicNo3);
            DicIntMusicNos.Add(4,  intMusicNo4);
            DicIntMusicNos.Add(5,  intMusicNo5);
            DicIntMusicNos.Add(6,  intMusicNo6);
            DicIntMusicNos.Add(7,  intMusicNo7);
            DicIntMusicNos.Add(8,  intMusicNo8);
            DicIntMusicNos.Add(9,  intMusicNo9);
            DicIntMusicNos.Add(10, intMusicNo10);
            DicIntMusicNos.Add(11, intMusicNo11);
            DicIntMusicNos.Add(12, intMusicNo12);

            DicSwMusicNFs.Add(1,  swMusicNF1);
            DicSwMusicNFs.Add(2,  swMusicNF2);
            DicSwMusicNFs.Add(3,  swMusicNF3);
            DicSwMusicNFs.Add(4,  swMusicNF4);
            DicSwMusicNFs.Add(5,  swMusicNF5);
            DicSwMusicNFs.Add(6,  swMusicNF6);
            DicSwMusicNFs.Add(7,  swMusicNF7);
            DicSwMusicNFs.Add(8,  swMusicNF8);
            DicSwMusicNFs.Add(9,  swMusicNF9);
            DicSwMusicNFs.Add(10, swMusicNF10);
            DicSwMusicNFs.Add(11, swMusicNF11);
            DicSwMusicNFs.Add(12, swMusicNF12);

            DicIntMusictimes.Add(1,  intMusictime1);
            DicIntMusictimes.Add(2,  intMusictime2);
            DicIntMusictimes.Add(3,  intMusictime3);
            DicIntMusictimes.Add(4,  intMusictime4);
            DicIntMusictimes.Add(5,  intMusictime5);
            DicIntMusictimes.Add(6,  intMusictime6);
            DicIntMusictimes.Add(7,  intMusictime7);
            DicIntMusictimes.Add(8,  intMusictime8);
            DicIntMusictimes.Add(9,  intMusictime9);
            DicIntMusictimes.Add(10, intMusictime10);
            DicIntMusictimes.Add(11, intMusictime11);
            DicIntMusictimes.Add(12, intMusictime12);

            DicSwMusicIsPlayAlongs.Add(1,  swMusicIsPlayAlong1);
            DicSwMusicIsPlayAlongs.Add(2,  swMusicIsPlayAlong2);
            DicSwMusicIsPlayAlongs.Add(3,  swMusicIsPlayAlong3);
            DicSwMusicIsPlayAlongs.Add(4,  swMusicIsPlayAlong4);
            DicSwMusicIsPlayAlongs.Add(5,  swMusicIsPlayAlong5);
            DicSwMusicIsPlayAlongs.Add(6,  swMusicIsPlayAlong6);
            DicSwMusicIsPlayAlongs.Add(7,  swMusicIsPlayAlong7);
            DicSwMusicIsPlayAlongs.Add(8,  swMusicIsPlayAlong8);
            DicSwMusicIsPlayAlongs.Add(9,  swMusicIsPlayAlong9);
            DicSwMusicIsPlayAlongs.Add(10, swMusicIsPlayAlong10);
            DicSwMusicIsPlayAlongs.Add(11, swMusicIsPlayAlong11);
            DicSwMusicIsPlayAlongs.Add(12, swMusicIsPlayAlong12);

            foreach (var area in DicTextAreaNames)
            {
                area.Value.RectColor = Color.White;
            }
        }

        /// <summary>
        /// 加载音乐设置
        /// </summary>
        private void LoadMusicSet()
        {
            try
            {
                LogObj.Info("加载音乐设置");
                musicSetList = musicSetBLL.GetModelAll();
                if (musicSetList != null && musicSetList.Count > 0)
                {
                    for (int i = 0; i < musicSetList.Count(); i++)
                    {
                        DicTextAreaNames[i + 1].Text = musicSetList[i].AreaName;
                        DicIntMusicNos[i + 1].Value = musicSetList[i].MusicId;
                        DicSwMusicNFs[i + 1].Active = musicSetList[i].MusicState;
                        DicIntMusictimes[i + 1].Value = musicSetList[i].PlayTimes;
                        DicSwMusicIsPlayAlongs[i + 1].Active = musicSetList[i].IsPlayAlong;
                    }
                }
            }
            catch (Exception ex)
            {
                LogObj.Error(ex.Message);
            }
        }

        /// <summary>
        /// 获取音乐设置
        /// </summary>
        private void GetMusicSet(ref bool isNew)
        {
            musicSetList = musicSetBLL.GetModelAll();

            if (musicSetList != null && musicSetList.Count > 0)
            {
                isNew = false;
                foreach (var item in musicSetList)
                {
                        int index = int.Parse(item.ID.ToString());
                        item.AreaName = DicTextAreaNames[index].Text;
                        item.MusicId = Convert.ToInt16(DicIntMusicNos[index].Value);
                        item.MusicState = DicSwMusicNFs[index].Active;
                        item.PlayTimes = DicIntMusictimes[index].Value;
                        item.IsPlayAlong = DicSwMusicIsPlayAlongs[index].Active;
                }
            }
            else
            {
                isNew = true;
                musicSetList = new List<MusicSetModel>();
                for (int i = 1;i<= DicTextAreaNames.Count;i++)
                {
                    MusicSetModel item = new MusicSetModel();
                    item.ID = i;
                    item.AreaType = (byte)i;
                    item.AreaName = DicTextAreaNames[i].Text;
                    item.MusicId = Convert.ToInt16(DicIntMusicNos[i].Value);
                    item.MusicState = DicSwMusicNFs[i].Active;
                    item.PlayTimes = DicIntMusictimes[i].Value;
                    item.IsPlayAlong = DicSwMusicIsPlayAlongs[i].Active;
                    musicSetList.Add(item);
                }
            }
        }

        private void btnMusicSetSave_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 0)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
            bool isNew = false;
            GetMusicSet(ref isNew);
            if (isNew)
            {
                if (musicSetBLL.AddList(musicSetList).IsSuccess)
                    UIMessageTip.ShowOk("保存成功");
                else
                    UIMessageBox.ShowError("保存失败");
            }
            else
            {
                if (musicSetBLL.UpdateList(musicSetList).IsSuccess)
                    UIMessageTip.ShowOk("保存成功");
                else
                    UIMessageBox.ShowError("保存失败");
            }
            //更新音乐参数缓存
            GolbalVariable.VoiceAreaMusicList = musicSetBLL.GetMusicBroadcastSet();
        }
    }
}
